//
//  RefreshTableView.m
//  HealthManager_Customer
//
//  Created by lzh on 2017/12/26.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "RefreshTableView.h"


@interface RefreshTableView()

@property (nonatomic, strong) UIView* noDataView;

@end


@implementation RefreshTableView

#pragma mark - override setter

- (void)setCanHeaderRefresh:(BOOL)canHeaderRefresh
{
    _canHeaderRefresh = canHeaderRefresh;
    
    if (canHeaderRefresh) {
        WeakSelf
        MJRefreshNormalHeader *he = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            StrongSelf
            if (self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(onRefreshing:)]) {
                [self.refreshDelegate onRefreshing:self];
            }
        }];
        he.lastUpdatedTimeLabel.hidden = YES;
        he.stateLabel.hidden = YES;
        self.mj_header = he;
//        self.mj_header =
    }
    else
    {
        self.mj_header = nil;
        
    }
}

- (void)setCanFooterRefresh:(BOOL)canFooterRefresh
{
    _canFooterRefresh = canFooterRefresh;
    if (canFooterRefresh) {
        WeakSelf
        self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            StrongSelf
            if (self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(onLoadingMoreData:)]) {
                [self.refreshDelegate onLoadingMoreData:self];
            }
        }];
        
        [self configFooterTitle:@"" state:MJRefreshStateNoMoreData];
        [self configFooterTitle:@"" state:MJRefreshStateIdle];

    }
    else
    {
        self.mj_footer = nil;
    }
}

- (void)setIsRefreshing:(BOOL)isRefreshing
{
    _isRefreshing = isRefreshing;
    
    if(_isRefreshing){
        if(![self.mj_header isRefreshing])
        {
            [self.mj_header beginRefreshing];
        }
    }
    else{
        
        if([self.mj_header isRefreshing])
        {
            [self.mj_header endRefreshing];
        }
    }
}


-(void)setIsLoadingMoreData:(BOOL)isLoadingMoreData{
    
    _isLoadingMoreData = isLoadingMoreData;
    if(_isLoadingMoreData){
        
        if(![self.mj_footer isRefreshing])
        {
            [self.mj_footer beginRefreshing];
        }
    }
    else{
        
        if([self.mj_footer isRefreshing])
        {
            [self.mj_footer endRefreshing];
        }
    }
}

- (void)setIsAllLoaded:(BOOL)isAllLoaded{
    _isAllLoaded = isAllLoaded;
    if(_isAllLoaded){
        [self.mj_footer endRefreshingWithNoMoreData];
    }else{
        [self.mj_footer resetNoMoreData];
    }
}


#pragma mark - nodataView

- (UIView*)noDataView
{
    if (!_noDataView) {
        
        _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, self.bounds.size.height)];
        
        NSString* noDataText = @"暂时没有数据";
        if (self.noDataText) {
            noDataText = self.noDataText;
        }
        
        _noDataView.backgroundColor = [UIColor whiteColor];
        
//        NoDataDefaultView* noDataDefaultView = [NoDataDefaultViewHelper noDataDefaultViewWithText:noDataText];
//        [_noDataView addSubview:noDataDefaultView];
        
    }
    
    return _noDataView;
}


- (void)showNoDataView
{
    [self addSubview:self.noDataView];
}

- (void)hideNoDataView
{
    [self.noDataView removeFromSuperview];
}


#pragma mark - config footer

- (void)configFooterTitle:(NSString*)title state:(MJRefreshState)state{
    if (self.mj_footer) {
        [((MJRefreshAutoNormalFooter*)(self.mj_footer)) setTitle:title forState:state];
    }
}

@end
